package com.naizhuai.network;

import android.content.Context;


import com.naizhuai.util.ConstantsParams;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by code_han on 2018/1/25.
 */

public class RetrofitServerFactory {

    private RetrofitServerFactory() {
    }

    private static volatile Map<String,Retrofit> RetrofitPool;


    public static Retrofit getInstance(Context context,String baseUrl,String privateKey){
        if (RetrofitPool==null){
            synchronized (RetrofitServerFactory.class){
                if (RetrofitPool==null){
                    RetrofitPool=new HashMap<>();
                }
            }
            RetrofitPool.put(baseUrl,createRetrofit(context,baseUrl,privateKey));
        }else{
            Retrofit getRetrofit=RetrofitPool.get(baseUrl);
            if(getRetrofit==null){
                RetrofitPool.put(baseUrl,createRetrofit(context,baseUrl,privateKey));
            }else{
                return getRetrofit;
            }
        }
        return RetrofitPool.get(baseUrl);
    }



    public static Retrofit getDownloadInstance(Context context,String baseUrl){
        if (RetrofitPool==null){
            synchronized (RetrofitServerFactory.class){
                if (RetrofitPool==null){
                    RetrofitPool=new HashMap<>();
                }
            }
            RetrofitPool.put(baseUrl,createDownloadRetrofit(context,baseUrl));
        }else{
            Retrofit getRetrofit=RetrofitPool.get(baseUrl);
            if(getRetrofit==null){
                RetrofitPool.put(baseUrl,createDownloadRetrofit(context,baseUrl));
            }else{
                return getRetrofit;
            }
        }
        return RetrofitPool.get(baseUrl);
    }

    private static Retrofit createRetrofit(Context context,String baseUrl,String privateKey) {
        return new Retrofit.Builder()
                .client(ClientFactory.getInstance(context,privateKey))
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    private static Retrofit createDownloadRetrofit(Context context,String baseUrl) {
        return new Retrofit.Builder()
                .client(ClientFactory.getDownloadInstance(context))
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    public static <T> T create(Context context,final Class<T> service,String baseUrl){
         switch (baseUrl){
             case Config.LOGIN_BASEURL:
                 return getInstance(context,baseUrl,ConstantsParams.LOGIN_KEY).create(service);
             default:
                 return getInstance(context,baseUrl,null).create(service);
         }
    }
    public static <T> T createDownload(Context context,final Class<T> service,String baseUrl){
         return getDownloadInstance(context,baseUrl).create(service);
    }
    public static <T> T createDownload(Context context,final Class<T> service){
        return getDownloadInstance(context,Config.COMPANY_COMMON_BASE_URL).create(service);
    }

}
